package com.ufo.store.management.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.ufo.store.management.R;
import com.ufo.store.management.entity.SerializableList;
import com.ufo.store.management.entity.SerializableMap;
import com.ufo.store.management.common.Config;

/**
 *  作者：taoli on 2017/2/27 0027 14:50
 *  
 */
public class HomeControllerFragment extends Fragment {
    private static final String TAG = HomeControllerFragment.class.getSimpleName();
    private View entryView;
    private SerializableMap mStockTypeMap = null;
    private SerializableMap mIsszmbalv3StockTypeMap = null;
    private SerializableList mIsszmbalv3BranchInfoList = null;
    private SerializableList mIsszmbalv3SupcustInfoList = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        entryView = inflater.inflate(R.layout.fragment_home_controller, container, false);
        entryView.findViewById(R.id.rl5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener!= null)
                    mListener.onItemClick(v,3);
            }
        });
        entryView.findViewById(R.id.rl2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( mIsszmbalv3StockTypeMap != null && mIsszmbalv3StockTypeMap.getMap() != null && mIsszmbalv3StockTypeMap.getMap().size() > 1) {
                    if(mListener!= null)
                        mListener.onItemClick(v,0);
                } else {
                    Toast.makeText(getActivity(), "尚未连接到思迅数据库，请检查连接", Toast.LENGTH_SHORT).show();
                }
            }
        });
        entryView.findViewById(R.id.rl3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( mIsszmbalv3StockTypeMap != null && mIsszmbalv3StockTypeMap.getMap() != null && mIsszmbalv3StockTypeMap.getMap().size() > 1) {
                    if(mListener!= null)
                        mListener.onItemClick(v,1);
                } else {
                    Toast.makeText(getActivity(), "尚未连接到数据库，请检查连接", Toast.LENGTH_SHORT).show();
                }
            }
        });
        entryView.findViewById(R.id.rl4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( mIsszmbalv3BranchInfoList != null && mIsszmbalv3BranchInfoList.getIsszmbalv3BranchInfoList() != null && mIsszmbalv3BranchInfoList.getIsszmbalv3BranchInfoList().size() > 0) {
                    if(mListener!= null)
                        mListener.onItemClick(v,2);
                }else{
                    Toast.makeText(getActivity(), "尚未连接到数据库，请检查连接", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return entryView;
    }

    public void setStockTypeMap(SerializableMap stockMap) {
        mStockTypeMap = stockMap;
//        if (mStockTypeMap != null && mStockTypeMap.getMap() != null && mStockTypeMap.getMap().size() > 1) {
//            long timeNow = System.currentTimeMillis();
//            long timeCheck = Config.getFastfoodBillCheckTime();
//            if(timeNow - timeCheck > 6 * 60 * 60 * 1000){
//                entryView.findViewById(R.id.bill_iv).setVisibility(View.VISIBLE);
//                Config.setFastfoodBillCheckTime(timeNow);
//            }else{
//                entryView.findViewById(R.id.bill_iv).setVisibility(View.GONE);
//            }
//        }
    }

    public void setIsszmbalv3StockTypeMap(SerializableMap isszmbalv3StockMap) {
        mIsszmbalv3StockTypeMap = isszmbalv3StockMap;
        if ( mIsszmbalv3StockTypeMap != null && mIsszmbalv3StockTypeMap.getMap() != null && mIsszmbalv3StockTypeMap.getMap().size() > 1) {
            long timeNow = System.currentTimeMillis();
            long timeCheck = Config.getIsszmbalv3BillCheckTime();
            if(timeNow - timeCheck > 6 * 60 * 60 * 1000){
                entryView.findViewById(R.id.isszmbalv3_bill_iv).setVisibility(View.VISIBLE);
                Config.setIsszmbalv3BillCheckTime(timeNow);
            }else{
                entryView.findViewById(R.id.isszmbalv3_bill_iv).setVisibility(View.GONE);
            }
        }
    }

    public void setIsszmbalv3BranchInfoList(SerializableList branchInfoList){
        mIsszmbalv3BranchInfoList = branchInfoList;
    }

    public void setIsszmbalv3SupcustInfoList(SerializableList supcustInfoList){
        mIsszmbalv3SupcustInfoList = supcustInfoList;
    }

    private static OnClickListener mListener;

    public void setOnClickListener(OnClickListener l) {
        mListener = l;
    }

    public interface OnClickListener {
        void onItemClick(View v, int position);
    }
}
